package me.zixing.timer.ui;

import me.zixing.timer.core.TimeComputUtil;
import me.zixing.timer.core.TimeSetUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Created by bingh on 2017/10/20.
 */
public class MainUI extends JFrame {

    private static Font font = new Font("黑体",Font.PLAIN,14);
    private static Insets insets = new Insets(0,0,0,0);
    private static volatile boolean move = true;
    private static DashBoardPanel dashBoardPanel = null;
    URL url = this.getClass().getClassLoader().getResource("1.wav");
    AudioClip audioClip = JApplet.newAudioClip(url);

    private static TrayIcon trayIcon = null;
    private static SystemTray tray = SystemTray.getSystemTray();

    JButton setButton = new JButton("设定");
    JButton startButton = new JButton("开始");
    JButton resetButton = new JButton("重置");
    JButton hiddenButton = new JButton("退出");

    public MainUI() throws HeadlessException {
        initUI();
        initAction();
    }

    private void initUI(){

        //创建mainPanel
        JPanel mainPanel = new JPanel();
        //创建表盘pannel
        dashBoardPanel = new DashBoardPanel();


        //设置mainPanel
        mainPanel.setBounds(0,0,320,70);
       // mainPanel.setBackground(Color.blue);
        mainPanel.setLayout(null);

        //设置表盘
        dashBoardPanel.setBounds(10,10,294,90);
        dashBoardPanel.setBackground(Color.black);
        dashBoardPanel.setLayout(null);
       // dashBoardPanel.setOpaque(true);

        //设置按钮
        setButton.setBounds(10,110,45,20);
        startButton.setBounds(65,110,45,20);
        resetButton.setBounds(202,110,45,20);
        hiddenButton.setBounds(258,110,45,20);
        setButton.setFont(font);
        startButton.setFont(font);
        resetButton.setFont(font);
        hiddenButton.setFont(font);
        setButton.setMargin(insets);
        startButton.setMargin(insets);
        resetButton.setMargin(insets);
        hiddenButton.setMargin(insets);


        //添加表盘
        mainPanel.add(dashBoardPanel);

        //添加按钮
        mainPanel.add(setButton);
        mainPanel.add(startButton);
        mainPanel.add(resetButton);
        mainPanel.add(hiddenButton);

        //添加mainPanel
        getContentPane().add(mainPanel);
    }


    public void initAction(){

        this.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {

                if(TimeComputUtil.set){
                    if (TimeSetUtil.index <= 1 || TimeSetUtil.index ==3 ){
                        switch(e.getKeyCode()){
                            //有效输入，记录到属性，index++
                            case KeyEvent.VK_0 :
                                TimeSetUtil.saveInputKey("0");
                                break;
                            case KeyEvent.VK_1 :
                                TimeSetUtil.saveInputKey("1");
                                break;
                            case KeyEvent.VK_2 :
                                TimeSetUtil.saveInputKey("2");
                                break;
                            case KeyEvent.VK_3 :
                                TimeSetUtil.saveInputKey("3");
                                break;
                            case KeyEvent.VK_4 :
                                TimeSetUtil.saveInputKey("4");
                                break;
                            case KeyEvent.VK_5 :
                                TimeSetUtil.saveInputKey("5");
                                break;
                            case KeyEvent.VK_6 :
                                TimeSetUtil.saveInputKey("6");
                                break;
                            case KeyEvent.VK_7 :
                                TimeSetUtil.saveInputKey("7");
                                break;
                            case KeyEvent.VK_8 :
                                TimeSetUtil.saveInputKey("8");
                                break;
                            case KeyEvent.VK_9 :
                                TimeSetUtil.saveInputKey("9");
                                break;
                            default:;

                        }
                    }else if(TimeSetUtil.index == 5){
                        switch (e.getKeyCode()){
                            //有效输入，记录到属性，保存到文件，属性清零，index清零，转为normal模式
                            case KeyEvent.VK_0 :
                                TimeSetUtil.saveInputKey("0");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_1 :
                                TimeSetUtil.saveInputKey("1");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_2 :
                                TimeSetUtil.saveInputKey("2");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_3 :
                                TimeSetUtil.saveInputKey("3");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_4 :
                                TimeSetUtil.saveInputKey("4");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_5 :
                                TimeSetUtil.saveInputKey("5");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_6 :
                                TimeSetUtil.saveInputKey("6");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_7 :
                                TimeSetUtil.saveInputKey("7");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_8 :
                                TimeSetUtil.saveInputKey("8");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            case KeyEvent.VK_9 :
                                TimeSetUtil.saveInputKey("9");
                                TimeSetUtil.setTimeToProperties();
                                TimeSetUtil.clear();
                                TimeComputUtil.normalOption();
                                MainUI.dashBoardPanel.repaint();
                                break;
                            default:;
                        }
                    }else if(TimeSetUtil.index == 2 || TimeSetUtil.index == 4){
                        switch (e.getKeyCode()){
                            //有效输入，记录到属性，index++
                            case KeyEvent.VK_0 :
                                TimeSetUtil.saveInputKey("0");
                                break;
                            case KeyEvent.VK_1 :
                                TimeSetUtil.saveInputKey("1");
                                break;
                            case KeyEvent.VK_2 :
                                TimeSetUtil.saveInputKey("2");
                                break;
                            case KeyEvent.VK_3 :
                                TimeSetUtil.saveInputKey("3");
                                break;
                            case KeyEvent.VK_4 :
                                TimeSetUtil.saveInputKey("4");
                                break;
                            case KeyEvent.VK_5 :
                                TimeSetUtil.saveInputKey("5");
                                break;
                            default:;
                        }
                    }

                }
            }
        });


        hiddenButton.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent e){
                System.exit(0);
                requestFocus();
            }
        });


        setButton.addActionListener(new ActionListener(){
            @Override public void actionPerformed(ActionEvent e){
                audioClip.stop();
                TimeComputUtil.setOption();
                Thread thread = new Thread(){
                    @Override
                    public void run() {
                        super.run();

                        while(TimeComputUtil.set){
                            try{
                                TimeUnit.MILLISECONDS.sleep(500);
                                dashBoardPanel.repaint();
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }
                };
                thread.start();

                requestFocus();
            }
        });

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                audioClip.stop();
                TimeComputUtil.resetOption();
                dashBoardPanel.repaint();
                requestFocus();
            }
        });

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                audioClip.stop();
                if(TimeComputUtil.reset == false && TimeComputUtil.normal == false){
                    return;
                }
                Thread thread = new Thread(){
                    @Override
                    public void run() {
                        super.run();

                       TimeComputUtil.startOption();
                       TimeComputUtil.computEndTime();
                        while(TimeComputUtil.start){
                            try{
                                TimeUnit.MILLISECONDS.sleep(500);
                                dashBoardPanel.repaint();
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        if(!TimeComputUtil.reset && !TimeComputUtil.set && !TimeComputUtil.normal){
                            audioClip.loop();
                        }

                    }
                };
                thread.start();


                /*Thread playThread = new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        while(TimeComputUtil.computTimeAsLong() > 0){
                            try{
                                TimeUnit.MILLISECONDS.sleep(500);
                                dashBoardPanel.repaint();
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                         if(!TimeComputUtil.reset && !TimeComputUtil.set && !TimeComputUtil.normal){
                            audioClip.loop();
                        }

                    }
                };
                playThread.start();*/
                requestFocus();
            }
        });

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowIconified(WindowEvent e) {
                MainUI.this.setExtendedState(JFrame.NORMAL);
                //mainUI.setVisible(false);
                //mainUI.miniTray();
            }
        });


        /*int x = (int) this.getLocation().getX();
        int y = (int) this.getLocation().getY();
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentMoved(ComponentEvent e) {
                if(move){
                    super.componentMoved(e);
                }else{
                    MainUI.this.setBounds(x,y,
                            MainUI.this.getWidth(),MainUI.this.getHeight());
                }
            }
        });*/

    }


    public static void main(String[] args) throws Exception{

        try
        {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            UIManager.put("RootPane.setupButtonVisible",false);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        MainUI mainUI = new MainUI();
        mainUI.setTitle("Timer");
        mainUI.setIconImage(ImageIO.read(MainUI.class.getClassLoader().getResource("timer.png")));
        mainUI.setSize(368,225);
        mainUI.setResizable(false);
        mainUI.setLocationRelativeTo(null);
        /*Toolkit toolkit = Toolkit.getDefaultToolkit();

        int x = (int)(toolkit.getScreenSize().getWidth()-mainUI.getWidth())/2;
        int y = (int)(toolkit.getScreenSize().getHeight()-mainUI.getHeight())/2;
        mainUI.setLocation(x, y);*/
        mainUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainUI.setVisible(true);
        mainUI.setFocusable(true);
        mainUI.setAlwaysOnTop(true);
        TimeComputUtil.normalOption();



    }

    private void miniTray(){  //窗口最小化到任务栏托盘
        ImageIcon trayImg = new ImageIcon(MainUI.class.getClassLoader().getResource("timer.png"));//托盘图标
        PopupMenu pop = new PopupMenu();  //增加托盘右击菜单
        MenuItem show = new MenuItem("show");
        MenuItem exit = new MenuItem("exit");
        show.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { // 按下还原键
                tray.remove(trayIcon);
                MainUI.this.setVisible(true);
                MainUI.this.setExtendedState(JFrame.NORMAL);
                MainUI.this.toFront();
            }
        });
        exit.addActionListener(new ActionListener() { // 按下退出键
            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon);
                System.exit(0);
            }
        });
        pop.add(show);
        pop.add(exit);
        trayIcon = new TrayIcon(trayImg.getImage(), "计时器", pop);
        trayIcon.setImageAutoSize(true);
        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) { // 鼠标器双击事件
                if (e.getClickCount() == 2) {
                    tray.remove(trayIcon); // 移去托盘图标
                    MainUI.this.setVisible(true);
                    MainUI.this.setExtendedState(JFrame.NORMAL); // 还原窗口
                    MainUI.this.toFront();
                }
            }
        });
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}
